//
//  AttributedIntString.m
//  DianDangDai
//
//  Created by bhf on 2017/12/19.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "AttributedIntString.h"

@implementation AttributedIntString

- (NSMutableAttributedString *)attributedStringWithString:(NSString *)string setTextColor:(UIColor *)textColor{
    
    NSString *temp =nil;
    NSMutableAttributedString *attrString;
    NSMutableAttributedString *attrString2 = [[NSMutableAttributedString alloc] init];
    UIFont *font;
    for(int i =0; i < [string length]; i++)
        
    {
        
        temp = [string substringWithRange:NSMakeRange(i,1)];
        NSLog(@"第%d个字是:%@",i,temp);
        
        if ([self isPureInt:temp]) {
            font = [UIFont systemFontOfSize:30.0];
            attrString = [[NSMutableAttributedString alloc] initWithString:temp];
            [attrString addAttribute:NSFontAttributeName value:font range:[temp rangeOfString:temp]];
            [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:@"#f42424"] range:[temp rangeOfString:temp]];
        } else {
            font = [UIFont systemFontOfSize:15.0];
            attrString = [[NSMutableAttributedString alloc] initWithString:temp];
            [attrString addAttribute:NSFontAttributeName value:font range:[temp rangeOfString:temp]];
            [attrString addAttribute:NSForegroundColorAttributeName value:textColor range:[temp rangeOfString:temp]];
        }
        [attrString2 appendAttributedString:attrString];
    }
    return attrString2;
}

//字符是否为数字

- (BOOL)isPureInt:(NSString *)string{
    
    NSScanner *scan = [NSScanner scannerWithString:string];
    
    int value;
    
    return [scan scanInt:&value] && [scan isAtEnd];
    
}

@end
